/**
 * 
 */
package com.app;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * from http://nadeausoftware.com/node/73#Code
 * @author 
 *
 */
public final class WebFile {

	// Saved response.
	private Map<String,List<String>> responseHeader = null;
	private URL responseURL = null;
	private int responseCode = -1;
	private String MIMEtype = null;
	private String charset = null;
	private Object content = null;
	
	public WebFile(String urlString) throws IOException{
	
		URL url = new URL(urlString);
		URLConnection ucon = url.openConnection();
		if(!(ucon instanceof HttpURLConnection))
				throw new IllegalArgumentException("URL protocol must be HTTP");
		
		HttpURLConnection httpcon = (HttpURLConnection)ucon;
		
		//set up a request
		httpcon.setConnectTimeout(10000);	//10 sec
		httpcon.setReadTimeout(10000);		//10 sec
		httpcon.setInstanceFollowRedirects(true);	//
		httpcon.setRequestProperty("User-agent", "spider");
		
		// Send the request
		httpcon.connect();
		
		// Get the response
		responseHeader = httpcon.getHeaderFields();
		responseCode = httpcon.getResponseCode();
		responseURL = httpcon.getURL();
		final int length = httpcon.getContentLength();
		final String type = httpcon.getContentType();
		
		if(type != null){
			final String[] parts = type.split(";");
			MIMEtype = parts[0].trim(); 
			for(int i = 1; i< parts.length && charset ==null; i++){
				final String t = parts[i].trim();
				final int index = t.toLowerCase().indexOf("charset=");
				if(index != -1)
					charset = t.substring(index+8);
				
			}			
		}
		
		// Get the content
		final InputStream stream = httpcon.getErrorStream();
		if(stream != null)
			content = readStream(length, stream);
		else if ((content = httpcon.getContent())!= null && content instanceof InputStream)
			content= readStream(length, (InputStream)content);
		
		httpcon.disconnect();
		
	}	
	
	 private Object readStream( int length, InputStream stream ) throws IOException{
		 
		 final int buflen = Math.max(1024, Math.max(length, stream.available()));
		 byte[] buf = new byte[buflen];
		 byte[] bytes = null;
		 
		 
		 
		 if(charset == null)
			 return bytes;
		 try{
			 return new String( bytes,charset);
		 }catch(java.io.UnsupportedEncodingException e){}
		 
		 return bytes;
	 }
	
	
	public Map<String, List<String>> getResponseHeader() {
		return responseHeader;
	}
	public URL getResponseURL() {
		return responseURL;
	}
	public int getResponseCode() {
		return responseCode;
	}
	public String getMIMEtype() {
		return MIMEtype;
	}
	public Object getContent() {
		return content;
	}
	
	
	
}
